#include "spi.h"
#include "ptdebug.h"
/* SPI bus device interface, compatible with RT-Thread 0.3.x/1.0.x */
static uint32_t _spi_bus_device_read(pt_device_t dev,
                                      int32_t    pos,
                                      void       *buffer,
                                      uint32_t   size)
{
    struct pt_spi_bus *bus;

    bus = (struct pt_spi_bus *)dev;
    PT_ASSERT(bus != PT_NULL);
    PT_ASSERT(bus->owner != PT_NULL);

    return pt_spi_transfer(bus->owner, PT_NULL, buffer, size);
}

static uint32_t _spi_bus_device_write(pt_device_t dev,
                                       int32_t    pos,
                                       const void *buffer,
                                       uint32_t   size)
{
    struct pt_spi_bus *bus;

    bus = (struct pt_spi_bus *)dev;
    PT_ASSERT(bus != PT_NULL);
    PT_ASSERT(bus->owner != PT_NULL);

    return pt_spi_transfer(bus->owner, buffer, PT_NULL, size);
}

static int32_t _spi_bus_device_control(pt_device_t dev,
                                        int         cmd,
                                        void       *args)
{
    /* TODO: add control command handle */
    switch (cmd)
    {
    case 0: /* set device */
        break;
    case 1: 
        break;
    }

    return PT_EOK;
}


int32_t pt_spi_bus_device_init(struct pt_spi_bus *bus, uint8_t device_id)
{
    struct pt_device *device;
    PT_ASSERT(bus != PT_NULL);

    device = &bus->parent;

    /* set device type */
    device->type    = PT_Device_Class_SPIBUS;
    /* initialize device interface */

    device->init    = PT_NULL;
    device->open    = PT_NULL;
    device->close   = PT_NULL;
    device->read    = _spi_bus_device_read;
    device->write   = _spi_bus_device_write;
    device->control = _spi_bus_device_control;


    /* register to device manager */
    return pt_device_register(device, device_id, PT_DEVICE_FLAG_RDWR);
}

/* SPI Dev device interface, compatible with RT-Thread 0.3.x/1.0.x */
static uint32_t _spidev_device_read(pt_device_t dev,
                                     int32_t    pos,
                                     void       *buffer,
                                     uint32_t   size)
{
    struct pt_spi_device *device;

    device = (struct pt_spi_device *)dev;
    PT_ASSERT(device != PT_NULL);
    PT_ASSERT(device->bus != PT_NULL);

    return pt_spi_transfer(device, PT_NULL, buffer, size);
}

static uint32_t _spidev_device_write(pt_device_t dev,
                                      int32_t    pos,
                                      const void *buffer,
                                      uint32_t   size)
{
    struct pt_spi_device *device;

    device = (struct pt_spi_device *)dev;
    PT_ASSERT(device != PT_NULL);
    PT_ASSERT(device->bus != PT_NULL);

    return pt_spi_transfer(device, buffer, PT_NULL, size);
}

static int32_t _spidev_device_control(pt_device_t dev,
                                       int         cmd,
                                       void       *args)
{
    switch (cmd)
    {
    case 0: /* set device */
        break;
    case 1: 
        break;
    }

    return PT_EOK;
}


int32_t pt_spi_dev_device_init(struct pt_spi_device *dev, uint8_t device_id)
{
    struct pt_device *device;
    PT_ASSERT(dev != PT_NULL);

    device = &(dev->parent);

    /* set device type */
    device->type    = PT_Device_Class_SPIDevice;

    device->init    = PT_NULL;
    device->open    = PT_NULL;
    device->close   = PT_NULL;
    device->read    = _spidev_device_read;
    device->write   = _spidev_device_write;
    device->control = _spidev_device_control;


    /* register to device manager */
    return pt_device_register(device, device_id, PT_DEVICE_FLAG_RDWR);
}
